<template>
  <div class="list-table">
    <el-form ref="queryForm" :model="queryParams" :inline="true">
      <el-form-item :label="$t('会议主题')" prop="meetingTitle">
        <el-input
          v-model="queryParams.meetingTitle"
          :placeholder="$t('会议主题')"
          clearable
          @keyup.enter.native="queryHandle"
        />
      </el-form-item>
      <el-form-item :label="$t('会议编号')" prop="meetingCode">
        <el-input
          v-model="queryParams.meetingCode"
          :placeholder="$t('会议编号')"
          clearable
          @keyup.enter.native="queryHandle"
        />
      </el-form-item>
      <el-form-item :label="$t('会议类型')" prop="meetingType">
        <el-select v-model="queryParams.meetingType" clearable :placeholder="$t('请选择')">
          <el-option
            v-for="(type, index) in mtTypeOptions"
            :key="index"
            :value="type.id"
            :label="$t(type.mtTypeLabel)"
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('会议时间')" prop="meetingTime" label-width="200">
        <DatePicker
          v-model="dateRange"
          type="daterange"
        />

      </el-form-item>
      <!--      <el-form-item :label="$t('项目类型')" prop="businessType">-->
      <!--        <el-select v-model="queryParams.businessType" clearable :placeholder="$t('请选择')">-->
      <!--          <el-option-->
      <!--            v-for="dict in businessTypeOptions"-->
      <!--            :key="dict.dictValue"-->
      <!--            :value="dict.dictValue"-->
      <!--            :label="$t(dict.dictLabel)"-->
      <!--          />-->
      <!--        </el-select>-->
      <!--      </el-form-item>-->
      <el-form-item :label="$t('项目名称')" prop="serviceId">
        <MainBodySelect
          v-model="queryParams.serviceId"
          parameters="deal"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="queryHandle">{{ $t("搜索") }}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t("重置") }}</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['oa:business:edit']"
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="addHandle"
        >{{ $t("新增") }}</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="mtBusinessList" border>
      <el-table-column :label="$t('序号')" type="index" align="center" show-overflow-tooltip width="50">
        <template slot-scope="scope">
          {{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('会议主题')" align="center" prop="meetingTitle" show-overflow-tooltip>
        <template slot-scope="scope">
          <el-link type="primary" @click="updateHandle(scope.row)">{{ scope.row.meetingTitle }}</el-link>
        </template>
      </el-table-column>
      <el-table-column :label="$t('会议编号')" align="center" prop="meetingCode" width="150" />
      <el-table-column :label="$t('会议类型')" align="center" prop="mtTypeLabel" width="130">
        <template slot-scope="scope">
          {{ $t(scope.row.mtTypeLabel) }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('开始日期')" align="center" prop="startTime" width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('项目名称')" align="center" show-overflow-tooltip min-width="200">
        <template slot-scope="scope">
          <span
            v-for="(ele, index) in scope.row.mtBusinessDetailList"
            :key="index"
          >{{ ele.serviceName }}{{ index === scope.row.mtBusinessDetailList.length - 1 || ele.serviceName == null || scope.row.mtBusinessDetailList[index + 1].serviceName == null ? '' : '/' }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('确认状态')" align="center" prop="confirmStatus" width="230">
        <template slot-scope="scope">
          <el-link :underline="false" @click="meetingConfirmedMethod(scope.row)">
            <span style="margin-right:5px;">{{ $t('参会') }}  {{ scope.row.confirmStatus.yes }}</span>
            <span style="margin-right:5px;">{{ $t('不参会') }}  {{ scope.row.confirmStatus.no }}</span>
            <span>{{ $t('未确认') }}  {{ scope.row.confirmStatus.unconfirmed }}</span>
          </el-link>
        </template>
      </el-table-column>
      <el-table-column :label="$t('操作')" fixed="right" align="center" class-name="small-padding fixed-width" width="280">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['oa:business:edit']"
            size="small"
            type="text"
            icon="el-icon-edit"
            @click="openMinutes(scope.row)"
          >{{ $t('会议纪要') }}</el-button>
          <el-button
            v-if="!scope.row.actStatus || scope.row.actStatus === 'Z'"
            v-hasPermi="['oa:business:edit']"
            size="small"
            type="text"
            icon="el-icon-edit"
            @click="updateHandle(scope.row)"
          >{{ $t('发送会议通知') }}</el-button>
          <el-button
            v-if="scope.row.actStatus"
            v-hasPermi="['oa:business:edit']"
            size="small"
            type="text"
            icon="el-icon-edit"
            @click="openVote(scope.row)"
          >{{ $t(!scope.row.voteActStatus ? '发起投票' : '查看') }}</el-button>
          <el-button
            v-hasPermi="['oa:business:remove']"
            size="small"
            type="text"
            icon="el-icon-delete"
            @click="deleteHandle(scope.row)"
          >{{ $t('删除') }}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改已发起会议对话框 -->
    <el-dialog :title="$t('会议信息')" :visible.sync="formOpen" :destroy-on-close="true" :close-on-click-modal="false" :lock-scroll="false" custom-class="dialogForm" append-to-body>
      <mtBusinessForm ref="mtBusinessForm" :business-detail-id-list="businessDetailIdList" @resetDetailList="resetDetailList" />
    </el-dialog>

    <el-dialog :title="$t('会议纪要')" :visible.sync="minutesOpen" :destroy-on-close="true" :close-on-click-modal="false" width="60%" :lock-scroll="false" custom-class="dialogForm" append-to-body>
      <mtMinutesForm ref="mtMinutesForm" :business-detail-id-list="businessDetailIdList" @resetDetailList="resetDetailList" />
    </el-dialog>

    <el-dialog :title="$t('投票表决')" :visible.sync="voteOpen" :destroy-on-close="true" :close-on-click-modal="false" width="60%" :lock-scroll="false" custom-class="dialogForm" append-to-body>
      <vote-form ref="voteForm" :business-detail-id-list="businessDetailIdList" @resetDetailList="resetDetailList" />
    </el-dialog>

    <!-- 投票用户选择 -->
    <el-dialog :title="$t('发起投票')" :visible.sync="voteUserOpen" width="500px" :close-on-click-modal="false" :lock-scroll="false" append-to-body custom-class="UserScope">
      <!--      <el-form ref="voteForm" :model="voteForm" :rules="voteRules" label-width="120px" class="dataForm">-->
      <!--        <el-form-item :label="$t('参与投票委员')" prop="voteUser">-->
      <!--          <UserSelect v-model="voteForm.voteUser" multiple />-->
      <!--        </el-form-item>-->
      <!--      </el-form>-->

      <div slot="footer" class="dialog-footer">
        <el-button @click="voteUserOpen = false">{{ $t("取消") }}</el-button>
        <el-button type="primary" @click="submitVote">{{ $t("确定") }}</el-button>
      </div>
    </el-dialog>

    <el-dialog :title="$t('会议确认结果')" :visible.sync="meetingConfirmedResults" :close-on-click-modal="false" :lock-scroll="false" append-to-body custom-class="meetingResults">
      <el-tabs v-if="viewMeetingResults.length > 0" v-model="active" v-loading="meetingConfirmedLoading" @tab-click="handleClick">
        <el-tab-pane v-for="(item, index) in viewMeetingResults" :key="index" :label="item.nickName" :name="item.nickName">
          <div class="meetingInfo" style="border-top: 1px solid #DFE6EC;">
            <el-col :span="24">
              <div class="el-form-item">
                <div class="el-form-item__label">
                  {{ $t('参会') }}
                </div>
                <div class="el-form-item__content">
                  {{ typeFormat(item.timeConfirm) }}
                </div>
              </div>
            </el-col>
            <el-col :span="24">
              <div class="el-form-item">
                <div class="el-form-item__label">
                  {{ $t('意见') }}
                </div>
                <div class="el-form-item__content">
                  {{ item.contextConfirm }}
                </div>
              </div>
            </el-col>
            <div v-for="(v, ind) in item.mtConfirmDealList" :key="ind" class="borderBottom">
              <el-col :span="24">
                <div class="el-form-item" style="padding: 10px 15px;">
                  <div>
                    {{ ind + 1 }}、
                    <span v-if="v.dealName">{{ v.dealName }}</span>
                    <span v-else>---</span>
                  </div>
                </div>
              </el-col>
              <el-col :span="24">
                <div class="el-form-item">
                  <div class="el-form-item__content" style="border: 0;">
                    <span v-if="v.remark">{{ v.remark }}</span>
                    <span v-else>---</span>
                  </div>
                </div>
              </el-col>
            </div>
          </div>
        </el-tab-pane>
      </el-tabs>

      <div v-else class="noneData">
        <div>
          <svg-icon icon-class="none" class-name="none-icon" />
          <p>{{ $t('暂无数据') }}</p>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listMtConfirmAll } from '@/api/oa/business/mtConfirm.js'
import { listMtBusiness, delMtBusiness, getMtBusiness } from '@/api/oa/business/mtBusiness'
import { startVote } from '@/api/oa/meeting/mtVote'
import mtBusinessForm from './mtBusinessForm.vue'
import mtMinutesForm from './mtMinutesForm.vue'
import voteForm from './voteForm.vue'
import { findAll } from '@/api/oa/type/mtType'
import MainBodySelect from '@/components/MainBodySelect/index'
import { downLoadExcelPost } from '@/utils/excelDownload'

export default {
  components: {
      mtBusinessForm,
      mtMinutesForm,
      MainBodySelect,
    voteForm
  },
  props: {
    businessDetailIdList: {
      type: Array,
      default: () => []
    }
  },
  data() {
      return {
          // 遮罩层
          loading: true,
          // 总条数
          total: 0,
          // 已发起会议表格数据
          mtBusinessList: [],
          // 是否显示弹出层
          formOpen: false,
        voteOpen: false,
          // 投票用户选择
          voteUserOpen: false,
          // 会议发起投票结果
          meetingConfirmedResults: false,
          meetingConfirmedLoading: false,
          // 会议纪要
          minutesOpen: false,
          // 会议类型
          mtTypeOptions: [],
          // 业务类型
          businessTypeOptions: [],
          // 会议开始时间
          dateRange: undefined,
          // 查询参数
          queryParams: {
              meetingTitle: undefined,
              meetingType: undefined,
              meetingCode: undefined,
              meetingStartTime: undefined,
              meetingEndTime: undefined,
              startDate: undefined,
              endDate: undefined,
              businessType: '',
              serviceId: undefined,
              pageNum: 1,
              pageSize: 10
          },
          // 表单参数
          form: {},
          // 发起投票页面
          voteForm: {
            // 会议ID
            meetingId: undefined,
            // 投票委员
            voteUser: []
          },
          // 表单校验
          rules: {
            meetingTitle: [
              { required: true, message: this.$t('必填信息'), trigger: 'blur' }
            ], meetingType: [
              { required: true, message: this.$t('必填信息'), trigger: 'blur' }
            ], startTime: [
              { required: true, message: this.$t('必填信息'), trigger: 'blur' }
            ], meetingTime: [
              { required: true, message: this.$t('必填信息'), trigger: 'blur' }
            ], participants: [
              { required: true, message: this.$t('必填信息'), trigger: 'blur' }
            ], secretary: [
              { required: true, message: this.$t('必填信息'), trigger: 'blur' }
            ]
          },
          // 发起投票表单校验
          voteRules: [],
          // 会议结果查看
          viewMeetingResults: [],
          typeOptions: [],
          active: ''
      }
  },
  created() {
      this.getList()
      findAll().then(response => {
        this.mtTypeOptions = response.data
      })
      this.getDicts('my_business_type').then(response => {
        this.businessTypeOptions = response.data
      })
      this.getDicts('sys_yes_no').then(response => {
        this.typeOptions = response.data
      })
  },
  methods: {
    handleClick() {

    },
    /** 查询已发起会议列表 */
    getList() {
        this.loading = true
        listMtBusiness(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
            this.mtBusinessList = response.rows
            this.total = response.total
            this.loading = false
        })
    },
    // 表单重置
    reset() {
        this.form = {
            id: undefined,
            meetingTitle: undefined,
            meetingType: undefined,
            startTime: undefined,
            meetingTime: undefined,
            participants: undefined,
            internalParticipants: undefined,
            meetingAddress: undefined,
            secretary: undefined,
            externalParticipants: undefined,
            meetingMainTitle: undefined,
            remark: undefined,
            createBy: undefined,
            createTime: undefined,
            updateBy: undefined,
            updateTime: undefined,
            delFlag: undefined
        }
        this.resetForm('form')
    },
    /** 搜索按钮操作 */
    queryHandle() {
        this.queryParams.pageNum = 1
        this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
        this.resetForm('queryForm')
        this.dateRange = undefined
        this.queryHandle()
    },
    /** 新增按钮操作 */
    addHandle() {
        this.formOpen = true
        this.$nextTick(() => {
            this.$refs.mtBusinessForm.init(null)
        })
    },
    /** 修改按钮操作 */
    updateHandle(row) {
        this.formOpen = true
        this.$nextTick(() => {
            // 调用form页面的 init 方法初始化数据
            this.$refs.mtBusinessForm.init(row)
        })
    },
    /** 删除按钮操作 */
    deleteHandle(row) {
        this.$confirm(this.$t('是否确认删除?'), this.$t('警告'), {
            confirmButtonText: this.$t('确定'),
            cancelButtonText: this.$t('取消'),
            type: 'warning'
        }).then(function() {
            return delMtBusiness(row.id)
        }).then(() => {
            this.getList()
            this.msgSuccess(this.$t('删除成功'))
        }).catch(function() {
        })
    },
    // 发起投票
    startVote(row) {
      this.voteUserOpen = true
      getMtBusiness(row.id).then(response => {
        this.voteForm.voteUser = []
        this.voteForm.meetingId = row.id
        var meetingData = response.data

        if (meetingData.participants != null) {
          for (var i = 0; i < meetingData.participants.length; i++) {
            this.voteForm.voteUser.push(meetingData.participants[i])
          }
        }
        if (meetingData.internalParticipants != null) {
          for (var j = 0; j < meetingData.internalParticipants.length; j++) {
            this.voteForm.voteUser.push(meetingData.internalParticipants[j])
          }
        }
      })
    },
    // 确认发起投票
    submitVote() {
      startVote(this.voteForm)
    },
    /** 导出按钮操作 */
    exportHandle() {
      downLoadExcelPost('/business/mtBusiness/export', this.queryParams)
    },
    resetDetailList() {
      this.$emit('resetDetailList')
    },
    // 打开会议纪要
    openMinutes(row) {
      this.minutesOpen = true
      this.$nextTick(() => {
        var minutesData = {}
        minutesData.meetingId = row.id
        this.$refs.mtMinutesForm.init(minutesData)
      })
    },
    // 打开投票
    openVote(row) {
      this.formOpen = true
      this.$nextTick(() => {
        // var voteData = {}
        // voteData.meetingId = row.id
        this.$refs.mtBusinessForm.init(row)
      })
    },
    // 查看会议确认结果
    meetingConfirmedMethod(row) {
      this.meetingConfirmedResults = true
      this.meetingConfirmedLoading = true
      listMtConfirmAll({ meetingId: row.id }).then(response => {
        if (response.code === 200 && response.data) {
          this.viewMeetingResults = response.data
          this.active = response.data[0].nickName
        } else {
          this.msgError(this.$t('数据获取异常!'))
        }
        this.meetingConfirmedLoading = false
      })
    },
    typeFormat(row) {
      return this.selectDictLabel(this.typeOptions, row)
    }

  }
}
</script>
